<template>
  <div class="login-contanar">
    <div class="login-box">
      <!-- logo部分开始 -->
      <div class="logo">
        <img src="../assets/logo.png" alt="">
      </div>
      <!-- 表单部分 -->
      <el-form :model="ruleForm" :rules="rules" ref="formName">
        <el-form-item prop="name">
          <el-input v-model="ruleForm.name" prefix-icon="fa fa-user-o" placeholder="请输入用户"></el-input>
        </el-form-item>
        <el-form-item prop="password">
            <el-input v-model="ruleForm.password" placeholder="请输入密码" :type="ispwd ? 'password' : 'text'">
              <i slot="prefix" :class="['fa','fa-lg', ispwd ? 'fa-eye' : 'fa-lock']" @click="ispwd=!ispwd"></i>
            </el-input>
        </el-form-item>
        <el-form-item class="btnlist">
          <el-button type="primary" @click="submitForm">登录</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
    data() {
      return {
        ispwd:true,
        ruleForm: {
          name: '',
          password: ''
        },
        rules: {
          name: [
            { required: true, message: '请输入合法名称', trigger: 'blur' },
            { min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入合法的密码', trigger: 'blur' },
            { min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
        }
      };
    },
    methods: {
      // 提交按钮时操作
      submitForm() {
        // 验证表单是否有效
        this.$refs.formName.validate(async (valid) => {
          if(!valid) return this.$message.error("不合法")
          // 有效,将表单的内容传递给后台,发送ajax,安装axios
          const {data:res} = await this.$http.post("login",{username:this.ruleForm.name,password:this.ruleForm.password})
          console.log(res)
          if(res.meta.status !== 200) return this.$message.error("请求数据失败")
          this.$message.success("登录成功")
          // 获取秘钥
          const token = res.data.token;
          // 将token存入sessionstorage中
          sessionStorage.setItem('token',token)
          // 通过编程时导航定位到home中
          this.$router.push("/home")
        });
      },
      // 重置
      resetForm() {
        this.$refs.formName.resetFields();
      }
    }
  }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
.login-contanar {
  width: 100%;
  height: 100%;
  background-color: #000;
  .login-box {
    padding: 0 16px;
    position: relative;
    top:  50%;
    left: 50%;
    transform: translate(-50%,-50%);
    width: 500px;
    height: 400px;
    background-color: #fff;
    border-radius: 10px;
    .el-form {
      margin-top: -40px;
      .btnlist {
        float: right;
      }
    }
  }
  .logo {
    position: relative;
    left: 50%;
    transform: translate(-50%,-40%);
    border: 10px solid #fff;
    box-shadow: 2px 2px 2px 2px rgba(134, 131, 131, 0.3);
    width: 200px;
    height: 200px;
    background-color: #ccc;
    border-radius: 50%;
    overflow: hidden;
    
  }
}
.fa-eye,.fa-lock {
  transition: 0.5s all ease;
  cursor: pointer;
  &:hover {
    color: red;
  }
}
</style>
